package xyz.jpcr.covid19app;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
import android.widget.Toast;

import java.text.NumberFormat;
import java.util.ArrayList;

import xyz.jpcr.covid19app.Activity.PerCityData;
import xyz.jpcr.covid19app.Model.Coll;
import xyz.jpcr.covid19app.Util.IDBHelper;

import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_CONFIRMED;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_CURED;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_CURRENT_CONFIRMED;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_DEAD;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_LAST_UPDATE_TIME;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_NAME;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_SUSPECTED;

public class ListWidgetService extends RemoteViewsService {


    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new ListRemoteViewFactory(this.getApplicationContext(), intent);
    }

    private class ListRemoteViewFactory implements RemoteViewsFactory {

        private Context mContext;
        private int mAppWidgetId;
        private ArrayList<Coll> collArrayList;
        private IDBHelper idbHelper;

        public ListRemoteViewFactory(Context context, Intent intent) {
            mContext = context;
            mAppWidgetId =  intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        @Override
        public void onCreate() {
            Log.i(mContext.toString(), "onCreate: create success");
            idbHelper = new IDBHelper(mContext);
            collArrayList = idbHelper.getAll();
        }

        @Override
        public void onDataSetChanged() {
            collArrayList = idbHelper.getAll();
        }

        @Override
        public void onDestroy() {

        }

        @Override
        public int getCount() {
            return collArrayList.size();
        }

        @Override
        public RemoteViews getViewAt(int position) {
            Coll coll = collArrayList.get(position);
            RemoteViews rv = new RemoteViews(getPackageName(), R.layout.widget_listitem);
            rv.setTextViewText(R.id.widget_name_tv, coll.getName());
            rv.setTextViewText(R.id.widget_cases_tv, NumberFormat.getInstance().format(coll.getConfirmedCount()));
            Intent intent = new Intent(mContext, PerCityData.class);
            intent.putExtra(CITY_NAME, coll.getName());
            intent.putExtra(CITY_CURRENT_CONFIRMED, coll.getCurrentConfirmedCount());
            intent.putExtra(CITY_SUSPECTED, coll.getSuspectedCount());
            intent.putExtra(CITY_CURED, coll.getCuredCount());
            intent.putExtra(CITY_DEAD, coll.getDeadCount());
            intent.putExtra(CITY_CONFIRMED, coll.getConfirmedCount());
            intent.putExtra(CITY_LAST_UPDATE_TIME, coll.getUpdateTime());
            Log.i(mContext.toString(), "getViewAt: "+position + " " + coll.toString());
            rv.setOnClickFillInIntent(R.id.widget_ll, intent);
            return rv;
        }

        @Override
        public RemoteViews getLoadingView() {
            return null;
        }

        @Override
        public int getViewTypeCount() {
            return 1;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }
    }
}
